from django.contrib.auth.models import AbstractUser
from django.db import models

# 备注的长度
from django.utils import timezone

COMMENT_DEFAULT_LENGTH = 100


# 用户
class User(AbstractUser):
    # 权限
    AUTH_NONE = 0
    AUTH_READ = 1
    AUTH_EDIT = 2
    AUTH_CHOICES = [
        (AUTH_NONE, '无权限'),
        (AUTH_READ, '只读'),
        (AUTH_EDIT, '编辑'),
    ]

    # 原料供应商信息
    material_suppplier_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                                  db_column='原料供应商信息的权限', verbose_name='原料供应商信息的权限')

    # 加工商信息
    processor_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                         db_column='加工商信息的权限', verbose_name='加工商信息的权限')

    # 原料信息
    material_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                        db_column='原料信息的权限', verbose_name='原料信息的权限')

    # 部门信息
    apartment_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                         db_column='部门信息的权限', verbose_name='部门信息的权限')

    # 信息查询
    query_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                     db_column='信息查询的权限', verbose_name='信息查询的权限')

    # 仓库库存
    repertory_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                         db_column='仓库库存的权限', verbose_name='仓库库存的权限')

    # 加工原料结存
    process_material_remain = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                                  db_column='加工原料结存的权限', verbose_name='加工原料结存的权限')

    # 历史车间领用
    history_workshop_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                                db_column='历史车间领用的权限', verbose_name='历史车间领用的权限')

    # 成本核算
    cost_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                    db_column='成本核算的权限', verbose_name='成本核算的权限')

    # 应付款
    pay_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False, db_column='应付款的权限',
                                   verbose_name='应付款的权限')

    # 历史单据
    history_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                       db_column='历史单据的权限', verbose_name='历史单据的权限')

    # 统计
    statistic_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                         db_column='统计的权限', verbose_name='统计的权限')

    # 入库单
    repertory_bill_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                              db_column='入库单的权限', verbose_name='入库单的权限')
    # 付款单
    pay_bill_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                        db_column='付款单的权限', verbose_name='付款单的权限')

    # 领用单
    receive_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                       db_column='领用单的权限', verbose_name='领用单的权限')

    # 采购即委外加工单
    buy_process_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                           db_column='采购即委外加工单的权限', verbose_name='采购即委外加工单的权限')

    # 退回单
    return_info = models.IntegerField(choices=AUTH_CHOICES, default=AUTH_NONE, blank=False, null=False,
                                      db_column='退回单的权限', verbose_name='退回单的权限')

    class Meta:
        db_table = '用户'
        verbose_name = verbose_name_plural = db_table

    def __str__(self):
        return self.username

    @classmethod
    def get_all(cls):
        return cls.objects.all()


# 供应商
class Supplyer(models.Model):
    # 公司名称
    company_name = models.CharField(max_length=50, blank=False, null=False, db_column='公司名称', verbose_name='公司名称')

    # 公司简称
    company_short_name = models.CharField(max_length=20, blank=False, null=False, db_column='公司简称', verbose_name='公司简称')

    # 公司地址
    company_address = models.CharField(max_length=150, blank=False, null=False, db_column='公司地址', verbose_name='公司地址')

    # 公司电话号码
    company_phone_number = models.CharField(max_length=11, blank=False, null=False, db_column='公司电话号码',
                                            verbose_name='公司电话号码')

    # 传真号码
    fax = models.CharField(max_length=12, blank=False, null=False, db_column='传真', verbose_name='传真')

    # 纳税人识别码
    taxpayer_code = models.CharField(max_length=15, blank=False, null=False, db_column='纳税人识别码', verbose_name='纳税人识别码')

    # 账号
    num = models.CharField(max_length=30, blank=False, null=False, db_column='账号', verbose_name='账号')

    # 其他账户
    other_account = models.CharField(max_length=30, blank=False, null=False, db_column='其他账户', verbose_name='其他账户')

    # 户名
    account_name = models.CharField(max_length=10, blank=False, null=False, db_column='户名', verbose_name='户名')

    # 开户行
    account_bank = models.CharField(max_length=30, blank=False, null=False, db_column='开户行', verbose_name='开户行')

    # 联系人
    linkman = models.CharField(max_length=10, blank=False, null=False, db_column='联系人', verbose_name='联系人')

    # 联系人电话号码
    linkman_phone_number = models.CharField(max_length=11, blank=False, null=False, db_column='联系人电话号码',
                                            verbose_name='联系人电话号码')

    # 联系人手机号码
    linkman_mobile_number = models.CharField(max_length=11, blank=False, null=False, db_column='联系人手机号码',
                                             verbose_name='联系人手机号码')

    # 备注一
    comment_one = models.CharField(max_length=COMMENT_DEFAULT_LENGTH, blank=True, null=True, db_column='备注一',
                                   verbose_name='备注一')

    # 备注二
    comment_two = models.CharField(max_length=COMMENT_DEFAULT_LENGTH, blank=True, null=True, db_column='备注二',
                                   verbose_name='备注二')

    def __str__(self):
        return self.company_name

    @classmethod
    def get_all(cls):
        return cls.objects.all()

    class Meta:
        db_table = '供应商'
        verbose_name = verbose_name_plural = db_table


# 原料
class Material(models.Model):
    # 原料品种及规格
    material_kind_size = models.CharField(max_length=30, blank=False, null=False, db_column='原料品种及规格',
                                          verbose_name='原料品种及规格')

    # 供应商
    supplyer = models.ForeignKey(Supplyer, on_delete=models.CASCADE, db_column='供应商', verbose_name='供应商')

    # 白胚线品种及规格
    line_kind_size = models.CharField(max_length=20, blank=False, null=False, db_column='白胚线品种及规格',
                                      verbose_name='白胚线品种及规格')

    # 批号
    number = models.CharField(max_length=30, blank=False, null=False, db_column='批号', verbose_name='批号')

    # 原料等级
    material_level = models.CharField(max_length=10, blank=False, null=False, db_column='原料等级', verbose_name='原料等级')

    # 备注一
    comment_one = models.CharField(max_length=COMMENT_DEFAULT_LENGTH, blank=True, null=True, db_column='备注一',
                                   verbose_name='备注一')

    # 备注二
    comment_two = models.CharField(max_length=COMMENT_DEFAULT_LENGTH, blank=True, null=True, db_column='备注二',
                                   verbose_name='备注二')

    class Meta:
        db_table = '原料'
        verbose_name = verbose_name_plural = db_table

    @classmethod
    def get_all(cls):
        return cls.objects.all()

    def __str__(self):
        return self.material_kind_size


# 加工商
class Processor(Supplyer):
    class Meta:
        db_table = '加工商'
        verbose_name = verbose_name_plural = db_table


# 车间
class WorkShop(models.Model):
    # 车间状态 0：空闲 1：被占用
    STATUS_OFF = 0
    STATUS_ON = 1

    STATUS_CHOICES = (
        (STATUS_OFF, '空闲'),
        (STATUS_ON, '被占用'),
    )

    # 车间名称
    name = models.CharField(max_length=30, blank=False, null=False, db_column='车间名称', verbose_name='车间名称')

    # 车间状态
    status = models.IntegerField(choices=STATUS_CHOICES, default=STATUS_OFF, blank=False, null=False, db_column='车间状态',
                                 verbose_name='车间状态')

    @classmethod
    def get_all(cls):
        return cls.objects.all()

    def __str__(self):
        return self.name

    class Meta:
        db_table = '车间'
        verbose_name = verbose_name_plural = db_table


# 入库记录
class RepertoryRecords(models.Model):
    # 原料
    material = models.ForeignKey(Material, on_delete=models.CASCADE, db_column='原料', verbose_name='原料')

    # 入库类型
    entry_type = models.CharField(max_length=30, blank=False, null=False, db_column='入库类型', verbose_name='入库类型')

    # 收货部门
    department = models.ForeignKey(WorkShop, on_delete=models.CASCADE, db_column='收货部门', verbose_name='收货部门')

    # 收货日期
    date = models.DateTimeField(default=timezone.now, db_column='收货日期', verbose_name='收获日期')

    # 一等品重量
    class_one_weight = models.CharField(max_length=10, blank=False, null=False, db_column='一等品重量', verbose_name='一等品重量')

    # 二等品重量
    class_two_weight = models.CharField(max_length=10, blank=False, null=False, db_column='二等品重量', verbose_name='二等品重量')

    # 油丝重量
    oil_silk_weight = models.CharField(max_length=10, blank=False, null=False, db_column='油丝重量', verbose_name='油丝重量')

    # 其他重量
    other_weight = models.CharField(max_length=10, blank=False, null=False, db_column='其他重量', verbose_name='其他重量')

    # 单价
    price = models.FloatField(max_length=10, blank=False, null=False, db_column='单价', verbose_name='单价')

    # 总金额
    sum_cost = models.FloatField(max_length=10, blank=False, null=False, db_column='总金额', verbose_name='总金额')

    # 本次付款金额
    cur_cost = models.FloatField(max_length=10, blank=False, null=False, db_column='本次付款金额', verbose_name='本次付款金额')

    # 实付运费
    carriage = models.FloatField(max_length=10, blank=False, null=False, db_column='实付运费', verbose_name='实付运费')

    # 备注一
    comment_one = models.CharField(max_length=COMMENT_DEFAULT_LENGTH, blank=False, null=False, db_column='备注一',
                                   verbose_name='备注一')

    # 备注二
    comment_two = models.CharField(max_length=COMMENT_DEFAULT_LENGTH, blank=False, null=False, db_column='备注二',
                                   verbose_name='备注二')

    @classmethod
    def get_all(cls):
        return cls.objects.all()

    class Meta:
        db_table = '入库记录'
        verbose_name = verbose_name_plural = db_table


# 领用记录
class ReceiveRecords(models.Model):
    # 车间
    workshop = models.ForeignKey(WorkShop, on_delete=models.CASCADE, db_column='车间', verbose_name='车间')

    # 供应商
    supplyer = models.ForeignKey(Supplyer, on_delete=models.CASCADE, db_column='供应商', verbose_name='供应商')

    # 原料
    material = models.ForeignKey(Material, on_delete=models.CASCADE, db_column='原料', verbose_name='原料')

    # 领用日期
    receive_date = models.DateTimeField(default=timezone.now, blank=False, null=False, db_column='领用日期',
                                        verbose_name='领用日期')

    # 一等品重量
    class_one_weight = models.CharField(max_length=10, blank=False, null=False, db_column='一等品重量', verbose_name='一等品重量')

    # 二等品重量
    class_two_weight = models.CharField(max_length=10, blank=False, null=False, db_column='二等品重量', verbose_name='二等品重量')

    # 油丝重量
    oil_silk_weight = models.CharField(max_length=10, blank=False, null=False, db_column='油丝重量', verbose_name='油丝重量')

    # 其他重量
    other_weight = models.CharField(max_length=10, blank=False, null=False, db_column='其他重量', verbose_name='其他重量')

    # 备注一
    comment_one = models.CharField(max_length=COMMENT_DEFAULT_LENGTH, blank=False, null=False, db_column='备注一',
                                   verbose_name='备注一')

    # 备注二
    comment_two = models.CharField(max_length=COMMENT_DEFAULT_LENGTH, blank=False, null=False, db_column='备注二',
                                   verbose_name='备注二')

    class Meta:
        db_table = '领用记录'
        verbose_name = verbose_name_plural = db_table

    @classmethod
    def get_all(cls):
        return cls.objects.all()


# 付款记录
class PayRecords(models.Model):
    # 收款单位
    payee = models.CharField(max_length=100, blank=False, null=False, db_column='收款单位', verbose_name='收款单位')

    # 收款账户
    account = models.CharField(max_length=100, blank=False, null=False, db_column='收款账户', verbose_name='收款账户')

    # 付款金额
    cost = models.FloatField(max_length=10, blank=False, null=False, db_column='付款金额', verbose_name='付款金额')

    # 付款日期
    date = models.DateTimeField(default=timezone.now, db_column='付款日期', verbose_name='付款日期')

    # 摘要
    abstract = models.CharField(max_length=100, blank=True, null=True, db_column='摘要', verbose_name='摘要')

    # 备注
    comment = models.CharField(max_length=100, blank=True, null=True, db_column='备注', verbose_name='备注')

    class Meta:
        db_table = '付款记录'
        verbose_name = verbose_name_plural = db_table

    def __str__(self):
        return self.payee

    @classmethod
    def get_all(cls):
        return cls.objects.all()


# 采购记录
class BuyRecords(models.Model):
    # 采购日期
    date = models.DateTimeField(default=timezone.now, blank=False, null=False, db_column='采购日期', verbose_name='采购日期')

    # 原料
    material = models.ForeignKey(Material, on_delete=models.CASCADE, db_column='原料', verbose_name='原料')

    # 加工商
    processor = models.ForeignKey(Processor, on_delete=models.CASCADE, db_column='加工商', verbose_name='加工商')

    # 采购数量
    num = models.CharField(max_length=10, blank=False, null=False, db_column='采购数量', verbose_name='采购数量')

    # 单包重量
    each_weight = models.FloatField(max_length=10, blank=False, null=False, db_column='单包重量', verbose_name='单包重量')

    # 采购重量
    buy_weight = models.FloatField(max_length=10, blank=False, null=False, db_column='采购重量', verbose_name='采购重量')

    # 单价
    price = models.FloatField(max_length=10, blank=False, null=False, db_column='单价', verbose_name='单价')

    # 运费
    carriage = models.FloatField(max_length=10, blank=False, null=False, db_column='运费', verbose_name='运费')

    # 应付金额
    sum_cost = models.FloatField(max_length=10, blank=False, null=False, db_column='应付金额', verbose_name='应付金额')

    # 实付金额
    real_cost = models.FloatField(max_length=10, blank=False, null=False, db_column='实付金额', verbose_name='实付金额')

    # 加工损耗率
    process_cost_rate = models.CharField(max_length=10, blank=False, null=False, db_column='加工损耗率',
                                         verbose_name='加工损耗率')

    # 塑料托盘个数
    tray_num = models.IntegerField(default=0, db_column='塑料托盘个数', verbose_name='塑料托盘个数')

    # 备注一
    comment_one = models.CharField(max_length=COMMENT_DEFAULT_LENGTH, blank=False, null=False, db_column='备注一',
                                   verbose_name='备注一')

    # 备注二
    comment_two = models.CharField(max_length=COMMENT_DEFAULT_LENGTH, blank=False, null=False, db_column='备注二',
                                   verbose_name='备注二')

    @classmethod
    def get_all(cls):
        return cls.objects.all()

    class Meta:
        db_table = '采购记录'
        verbose_name = verbose_name_plural = db_table


# 退货记录
class ReturnGoodsRecords(models.Model):
    # 原料
    material = models.ForeignKey(Material, on_delete=models.CASCADE, db_column='原料', verbose_name='原料')

    # 加工商
    processor = models.ForeignKey(Processor, on_delete=models.CASCADE, db_column='加工商', verbose_name='加工商')

    # 数量
    number = models.CharField(max_length=10, blank=False, null=False, db_column='数量', verbose_name='数量')

    # 退货日期
    date = models.DateTimeField(default=timezone.now, blank=False, null=False, db_column='退货日期', verbose_name='退货日期')

    # 退货原因
    reason = models.CharField(max_length=200, blank=False, null=False, db_column='退货原因', verbose_name='退货原因')

    # 备注一
    comment_one = models.CharField(max_length=COMMENT_DEFAULT_LENGTH, blank=False, null=False, db_column='备注一',
                                   verbose_name='备注一')

    # 备注二
    comment_two = models.CharField(max_length=COMMENT_DEFAULT_LENGTH, blank=False, null=False, db_column='备注二',
                                   verbose_name='备注二')

    @classmethod
    def get_all(cls):
        return cls.objects.all()

    class Meta:
        db_table = '退货记录'
        verbose_name = verbose_name_plural = db_table
